<?php
namespace app\controller;

use app\model\study\StudyModel;

class Admin
{
    public function read() {
        $p = input('post.p');

        if ($p == "") return "";

        return file_get_contents($p);
    }
    public function save() {
        $p = input('post.p');

        if ($p == "") return "";

        return file_put_contents($p,input("post.c"));
    }
    public function new_post_type() {
        $p = input('post.p');
        $c = input("post.c");

        if ($p == "" || $c == "") return json(["msg"=>"请输入名称"]);

        $u = new \app\model\post\TypeModel();
        $u["chinese"] = $p;
        $u["english"] = $c;
        $u->save();

        return json(["msg"=>"新建成功"]);
    }
    public function new_money_type() {
        $p = input('post.p');
        $c = input("post.c");

        if ($p == "" || $c == "") return json(["msg"=>"请输入名称"]);

        $u = new \app\model\money\TypeModel();
        $u["chinese"] = $p;
        $u["english"] = $c;
        $u->save();

        return json(["msg"=>"新建成功"]);
    }
    public function new_study() {
        $t = input('post.t');
        $c = input("post.c");
        $b = input("post.b");

        if ($b == "" || $c == "" || $t == "") return json(["msg"=>"请输入名称"]);

        $data = [];

        switch ($t) {
            case "bili":
                $data["bv"] = $b;
                break;
        }

        $s = new StudyModel();
        $s->name = $c;
        $s->type = $t;
        $s->content = json_encode($data);
        $s->save();
        return json(["msg"=>"添加成功"]);
    }
}